package com.liaow.test.retrofit.testbeanconfiguration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: Spring注解之@Configuration和@Bean使用
 * 启动时，往IOC容器中注入Bean：mysqlDataSource、oracleDataSource、commonDataSource
 * EnableConfigurationProperties：可以保证先创建YmlPropertiesConfig这个Bean并注入IOC，再创建DataSourceConfig这个Bean，
 * 这样 在Autowired才能拿到ymlPropertiesConfig
 * @Author: L.Wen
 * @Creation_Time: 2020/08/25 13:40
 */
@Configuration
@EnableConfigurationProperties(YmlPropertiesConfig.class)
public class DataSourceConfig {

    @Autowired
    private YmlPropertiesConfig ymlPropertiesConfig;

    /**
     * method.invoke()方式创建
     * 即 反射的方式
     *
     * @return
     */
    @Bean
    public MysqlDataSource mysqlDataSource() {
        return new MysqlDataSource();
    }

    @Bean(name = "oracleDataSource")
    public OracleDataSource oracleDataSource() {
        return new OracleDataSource();
    }

    @Bean
    public CommonDataSource commonDataSource() {
        CommonDataSource commonDataSource = new CommonDataSource();
        commonDataSource.setMysqlDataSource(mysqlDataSource());
        commonDataSource.setOracleDataSource(oracleDataSource());
        commonDataSource.setServerPort(ymlPropertiesConfig.getServerPort());
        return commonDataSource;
    }
}
